package com.example.demo;



import com.example.demo.config.InitAuth;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.*;
import java.nio.charset.Charset;


@SpringBootApplication
public class DemoApplication {

    @Autowired
    private InitAuth initAuth;

    public static void main(String[] args) throws IOException {
        SpringApplication.run(DemoApplication.class, args);


        String  fileContent= "我是写入的文件";
//        String outPutPath="/home/Recycle_module/";
        String outPutPath="C:\\Users\\kaifacs\\Downloads\\";

            String fileName = "TBANK.7505.02250653100002908.76782869083143610368-D-018";
            String path = null;
            NtlmPasswordAuthentication auth = null;
        SmbFileOutputStream  sfos= null;
            SmbFile sFile = null;
            try {
                String USER_NAME = "administrator";
                String PASSWORD = "123456";
                String DOMAIN = null;
                String NETWORK_FOLDER = "smb://60.29.24.11/aaaa/";
                auth = new NtlmPasswordAuthentication(
                        DOMAIN, USER_NAME, PASSWORD);

                path = NETWORK_FOLDER + fileName;
                sFile = new SmbFile(path, auth);
                sfos = new SmbFileOutputStream(sFile);
                sfos.write(fileContent.getBytes());

                System.out.println("File successfully created.");

                SmbFile smbFile = new SmbFile(path, auth);

                SmbFileInputStream smbFileInputStream = new SmbFileInputStream(smbFile);


                byte[] buffer = new byte[1024];
            while (smbFileInputStream.read(buffer) != -1) {
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(outPutPath + fileName), Charset.forName("UTF-8"));

                DataOutputStream dataOutputStream;
                dataOutputStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outPutPath + fileName)));

                dataOutputStream.write(buffer);
                dataOutputStream.flush();

//                outputStreamWriter.write(new String(buffer));
//                outputStreamWriter.append(new String(buffer));
//                outputStream.write(buffer);
                System.out.println("success");
//                buffer = new byte[1024];

            }




            } catch (Exception e) {

                System.err.println("Unable to create file. Cause: "
                        + e);
            }







        }

    }
